package com.demo7;

import com.github.unidbg.Emulator;
import com.github.unidbg.file.linux.AndroidFileIO;
import com.github.unidbg.linux.ARM32SyscallHandler;
import com.github.unidbg.memory.SvcMemory;
import com.github.unidbg.unix.struct.TimeVal32;
import com.github.unidbg.unix.struct.TimeZone;
import com.sun.jna.Pointer;

import java.util.Calendar;


public class TDARM32SyscallHandler extends ARM32SyscallHandler {

    public TDARM32SyscallHandler(SvcMemory svcMemory) {
        super(svcMemory);
    }

    // 处理Unidbg gettimeofday的小 bug
    @Override
    protected int gettimeofday(Emulator<?> emulator, Pointer tv, Pointer tz) {
//        System.out.println("call mytime");
        long currentTimeMillis = System.currentTimeMillis();
        long tv_sec = currentTimeMillis / 1000;
        long tv_usec = (currentTimeMillis % 1000) * 1000;

        if (tv != null) {
            TimeVal32 timeVal = new TimeVal32(tv);
            timeVal.tv_sec = (int) tv_sec;
            timeVal.tv_usec = (int) tv_usec;
            timeVal.pack();
        }

        if (tz != null) {
            Calendar calendar = Calendar.getInstance();
            int tz_minuteswest = -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
            TimeZone timeZone = new TimeZone(tz);
            timeZone.tz_minuteswest = tz_minuteswest;
            timeZone.tz_dsttime = 0;
            timeZone.pack();
        }
        return 0;
    }

}
